home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13781 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.2 KB  |  97 lines

  1. Path: ix.netcom.com!news
  2. From: jlilley@ix.netcom.com (John Lilley)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: REPOST:  Template Class Nested Types
  5. Date: 27 Mar 1996 05:45:17 GMT
  6. Organization: Netcom
  7. Message-ID: <4jakld$d58@dfw-ixnews2.ix.netcom.com>
  8. References: <4j1c78$1b5@taco.cc.ncsu.edu>
  9. NNTP-Posting-Host: den-co10-01.ix.netcom.com
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=US-ASCII
  12. X-NETCOM-Date: Tue Mar 26 11:45:17 PM CST 1996
  13. X-Newsreader: WinVN 0.99.7
  14.  
  15. In article <4j1c78$1b5@taco.cc.ncsu.edu>, samebust@pop-in.ncsu.edu says...
  16. >
  17. >RE:  Template Class Nested Types:  Private member functions of the
  18. >     enclosing class that take parameters of the enclosed class type.
  19. >
  20. >... cause the compilers (GNU 2.7.0(?) under Linux,
  21. >Borland C++ 3.1) to choke.  They apparently don't recognize the Node
  22. >class (that is, it appears to be out of scope).  But, if I move the body
  23. >of these member functions into the Binary Tree class declaration (inline),
  24. >the compilers happily accept the code.  I have tried qualifying the
  25. >Node class with the Tree class name and template parameter list but this
  26. >didn't help (i.e. tree<type>::node ).
  27.  
  28. Your code compiles under Borland C++ 4.5, so I slate it as a compiler
  29. problem.  You might try making Node a templated class itself and removing
  30. it from the tree class:
  31.  
  32. // SNIPPET 2 //
  33.  
  34. #include <stddef.h>
  35.  
  36. template <class info>
  37. class node
  38. {
  39.   public:
  40.     info value_;
  41.     node *lchild_, *rchild_;
  42. };
  43.  
  44. template <class info>
  45. class tree
  46. {
  47.   public:
  48.     tree();
  49.     void Insert(const info &i);
  50.     //
  51.     // other member functions like Delete, etc.
  52.     //
  53.  
  54.   private:
  55.  
  56.     void Insert2(const info &i, node* &np);
  57.     //
  58.     // other private member functions
  59.     //
  60.  
  61.     node<info> *root_;
  62. };
  63.  
  64.  
  65. template <class info>
  66. tree<info>::tree()
  67. {
  68.   root_=NULL;
  69. }
  70.  
  71. template <class info>
  72. void tree<info>::Insert(const info &i)
  73. {
  74.   Insert2(i, root_);
  75. }
  76.  
  77. template <class info>
  78. void tree<info>::Insert2(const info &i, node<info>* &np)
  79. {
  80.   if (np==NULL)
  81.   {
  82.     np=new node;
  83.     // assert (np!=NULL);
  84.     np->lchild_=NULL;
  85.     np->rchild_=NULL;
  86.     np->value_=i;
  87.   }
  88.   else if (i<np->value_)
  89.     Insert2 (i,np->lchild_);
  90.   else if (i>np->value_)
  91.     Insert2 (i,np->rchild_);
  92. }
  93.  
  94.  
  95. john lilley
  96.  
  97.